package models

import (
	"github.com/astaxie/beego/orm"
	"strconv"
)

//获取角色列表
//@param pageNo 页码
//@param pageSize 页面大小
//@param beginTime 开始时间
//@param endTime 结束时间
//@param name 名称
func (*AdminRole) GetList (pageNo,pageSize int,beginTime,endTime,name string) ([]AdminRole,int,error) {
	var roles [] AdminRole
	var total int
	var sqlTemplet string
	totalSql := "SELECT COUNT(1) FROM admin_role "
	rowSql := "SELECT * FROM admin_role "
	lastSql := " order By createTime desc limit "+ strconv.Itoa((pageNo - 1) * pageSize) + ","+ strconv.Itoa(pageSize)
	if beginTime != "" || name != "" {
		sqlTemplet = " where 1=1 "
		if beginTime != "" && endTime != "" {
			sqlTemplet += " and createTime  >  str_to_date('"+ beginTime +"','%Y-%m-%d') and createTime < str_to_date('"+ endTime +"','%Y-%m-%d')"
		}
		if name != "" {
			sqlTemplet += " and name like '%"+name+"%'"
		}
	}
	err := orm.NewOrm().Raw(totalSql + sqlTemplet).QueryRow(&total)
	if err == nil && total > 0 {
		_,err = orm.NewOrm().Raw(rowSql + sqlTemplet + lastSql).QueryRows(&roles)
	}
	return roles,total,err
}
